How To Get Float With 2 Decimal Places In Java 您所在的位置:网站首页 fraction digits How To Get Float With 2 Decimal Places In Java

How To Get Float With 2 Decimal Places In Java

2023-03-19 04:12| 来源: 网络整理| 查看: 265

How to print a float with 2 decimal places in Java

In Java, float type represents a floating-point value聽that contains several digits after the decimal point. Although all the digits are important for mathematical computation but generally only two or three digits after the decimal point are significant.聽

In this article, we will learn to get a float value with 2 decimal places only. We will use several methods such as聽setMaximumFractionDigits(), format(), printf(), etc in our examples. Let's see the examples.

Example 1: Use printf() method to print the float value with 2 decimal place

Java provides printf()聽method to print formatted output to the console. It is similar to printf() method of C language, if you are familiar with聽it then you can easily relate to that. This method takes the first argument as a formatted string that specifies the output format.

/* * Example to print float value with 2 decimal points in Java */ public class JExercise { public static void main(String[] args) { float a = 1111; float b = 7; float result = a/b; // Display result System.out.println(result); // Prints only 2 decimal points System.out.printf("%.2f", result); } }

Output:

158.71428 158.71

Example 2: Use format() method to print the float value with 2 decimal place

In this example, we used the format() method of DecimalFormat class to get float value with 2 decimal points. You can see that we used the setMaximumFractionDigits() method to specify the fraction digit limit.聽

import java.text.DecimalFormat; /* * Example to print float value with 2 decimal points in Java */ public class JExercise { public static void main(String[] args) { float a = 1111; float b = 7; float result = a/b; // Display result System.out.println(result); DecimalFormat dfrmt = new DecimalFormat(); dfrmt.setMaximumFractionDigits(2); // Prints only 2 decimal points System.out.println(dfrmt.format(result)); } }

Output:

158.71428 158.71

Example 3: Use format() method of String class to print the float value with 2 decimal place

Java String class provides a format() method to format a value and returns a string which further can be converted into a float by using the valueOf() method of Float class.

/* * Example to print float value with 2 decimal points in Java */ public class JExercise { public static void main(String[] args) { float a = 1111; float b = 7; float result = a/b; // Display result System.out.println(result); String str = String.format("%.02f", result); result = Float.valueOf(str); // Prints only 2 decimal points System.out.print(result); } }

Output:

158.71428 158.71

Example 3: Use format("#.##") method to print the float value with 2 decimal place

The DecimalFormat class's constructor accepts a pattern that specifies the format of the output. You can see that by using this trick, we don't need to use the setMaximumFractionDigits() method. It works fine and produces the desired result.

import java.text.DecimalFormat; /* * Example to print float value with 2 decimal points in Java */ public class JExercise { public static void main(String[] args) { float a = 1111; float b = 7; float result = a/b; // Display result System.out.println(result); DecimalFormat dfrmt = new DecimalFormat("#.##"); // Prints only 2 decimal points System.out.println(dfrmt.format(result)); } }

Output:

158.71428 158.71

Conclusion

Well, in this topic, we have learned to get a float value with 2 decimal places. We used several methods and classes such as String class, DecimalFormat class, etc that provide format method to format the input into the desired output.

If we missed something, you can suggest us at - [email protected]



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有